package iwpl.project.servlet;

import iwpl.project.dao.ApartmentsDAO;
import iwpl.project.dao.AreasDAO;
import iwpl.project.dao.EquipmentDetailsDAO;
import iwpl.project.dao.EquipmentsDAO;
import iwpl.project.entities.Apartments;
import iwpl.project.entities.Areas;
import iwpl.project.entities.EquipmentDetails;
import iwpl.project.entities.Equipments;
import iwpl.project.entitymanagerhelper.EntityManagerHelper;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class Control_EquipmentDetail extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public Control_EquipmentDetail() {
		super();
	}
	
	public static void ExecuteControl(String Control, EquipmentDetails equipd)
	{
		//TODO add code here
		EquipmentDetailsDAO dao = new EquipmentDetailsDAO();
		
		if(Control == "Save")
		{
			EntityManagerHelper.beginTransaction();
			dao.save(equipd);
			EntityManagerHelper.commit();
		}
		
		if(Control == "Update")
		{
			EntityManagerHelper.beginTransaction();
			dao.update(equipd);
			EntityManagerHelper.commit();
		}
		
		if(Control == "Delete")
		{
			EntityManagerHelper.beginTransaction();
			dao.delete(equipd);
			EntityManagerHelper.commit();
		}
	}
	

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}
	
	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doProcess(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doProcess(request, response);
	}

	
	public String getSearch(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		String sql="";
		
		String cboApartmentNumber=request.getParameter("cboApartmentNumber");
		String cboEquipmentname=request.getParameter("cboEquipmentName");
		
		
		if(cboApartmentNumber != "")
		{
			sql = "where ApartmentID = "+cboApartmentNumber+" AND ";
		}
		else
		{
			sql = "where ";
		}
		if(cboEquipmentname != "" )
		{
			sql= sql  + "EquipmentID = "+cboEquipmentname+" AND ";
		}	
		
			int count = sql.length();
			sql = sql.substring(0 , count - 5);
		
		
		return sql;
		
		
	}
	public void doProcess(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
	{
		EquipmentDetails equipdetails = new EquipmentDetails();
		String sStatusDelete = request.getParameter("delete");
		String sStatusSearch = request.getParameter("btnSearch");
		if (sStatusSearch != null)
		{	
			EquipmentDetailsDAO dao= new EquipmentDetailsDAO();
			List<EquipmentDetails> list;
			list = dao.findWith(getSearch(request, response));
			
			HttpSession session = request.getSession(true);
			session.setAttribute("hehe", list);
			response.sendRedirect("../Manage_EquipmentDetails.jsp");
			
		}
		else
		{
		if(sStatusDelete != null)
		{	
			
			equipdetails.setId(Integer.parseInt(sStatusDelete));
			ExecuteControl("Delete", equipdetails);
			// After run command, return main page
			response.sendRedirect("/DepartmentManage/Manage_EquipmentDetails.jsp");
		
		}
		else
		{
		String sStatusInsert = request.getParameter("btnInsert");
		String sStatusUpdate = request.getParameter("btnUpdate");
		String sID = request.getParameter("hiddenid");
			if(sStatusInsert != null)
			{
				if(request.getParameter("cboEquipmentName")!= null)
				{
					Integer sApartmentID = Integer.parseInt(request.getParameter("cboApartmentNumber"));
					ApartmentsDAO dao = new ApartmentsDAO();
					Apartments apart = dao.findById(sApartmentID);
				
					Integer sEquipmentID =Integer.parseInt(request.getParameter("cboEquipmentName"));
					EquipmentsDAO dao1 = new EquipmentsDAO();
					Equipments equip =dao1.findById(sEquipmentID);
				
					equipdetails.setApartments(apart);
					equipdetails.setEquipments(equip);
								
				ExecuteControl("Save", equipdetails);
				// After run command, return main page
				response.sendRedirect("/DepartmentManage/Manage_EquipmentDetails.jsp");
				
				}
		}
					
		if (sStatusUpdate != null)
		{	
			if(request.getParameter("cboEquipmentName")!= null)
			{
			
			Integer sApartmentID = Integer.parseInt(request.getParameter("cboApartmentNumber"));
			ApartmentsDAO dao = new ApartmentsDAO();
			Apartments apart = dao.findById(sApartmentID);
		
			Integer sEquipmentID =Integer.parseInt(request.getParameter("cboEquipmentName"));
			EquipmentsDAO dao1 = new EquipmentsDAO();
			Equipments equip =dao1.findById(sEquipmentID);
			
			equipdetails.setId(Integer.parseInt(sID));
			equipdetails.setApartments(apart);
			equipdetails.setEquipments(equip);
			}
			
			ExecuteControl("Update", equipdetails);
			// After run command, return main page
			response.sendRedirect("/DepartmentManage/Manage_EquipmentDetails.jsp");
		}
			
		}}
		
		
		
		
	}
	
		
	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
